package com.aust.furniture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aust.furniture.entity.Furniture;
import com.aust.furniture.mapper.FurnitureMapper;
import com.aust.furniture.service.IFurnitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 家具service实现类
 */
@Service
public class FurnitureServiceImpl extends ServiceImpl<FurnitureMapper, Furniture> implements IFurnitureService {

    @Autowired
    private FurnitureMapper furnitureMapper;

    /**
     * 分页查询家具数据
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条数据
     * @param param    查询参数-家具名称
     * @return
     */
    @Override
    public IPage<Furniture> selectFurniturePage(int pageNum, int pageSize, String param) {
        QueryWrapper<Furniture> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("name",param);
        }
        Page<Furniture> page = new Page<>(pageNum,pageSize);
        return furnitureMapper.selectPage(page,queryWrapper);
    }

    /**
     * 新增一条家具信息
     *
     * @param furniture
     */
    @Override
    public int addFurniture(Furniture furniture) {
        return furnitureMapper.insert(furniture);
    }

    /**
     * 修改一条家具信息
     *
     * @param furniture
     */
    @Override
    public int editFurniture(Furniture furniture) {
        return furnitureMapper.updateById(furniture);
    }

    /**
     * 根据主键id查询一个家具对象
     *
     * @param id
     * @return
     */
    @Override
    public Furniture queryFurnitureById(Integer id) {
        return furnitureMapper.selectById(id);
    }

    /**
     * 根据主键id删除一个家具对象
     *
     * @param id
     * @return
     */
    @Override
    public int delFurnitureById(Integer id) {
        return furnitureMapper.deleteById(id);
    }

    /**
     * 查询所有家具
     *
     * @return
     */
    @Override
    public List<Furniture> queryFurnitureList() {
        return furnitureMapper.selectList(null);
    }
}
